/**
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.ui.action;

import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JTable;

import com.wateray.ipassbook.domain.form.ReceiptForm;
import com.wateray.ipassbook.ui.model.AbstractPassbookTableModel;
import com.wateray.ipassbook.ui.widget.PassbookTable;
import com.wateray.ipassbook.util.ImageManager;
import com.wateray.ipassbook.util.LanguageLoader;

/**
 * @author wateray
 * @create 2009-6-6
 */
public class ReceiptCopyAction extends AbstractAction {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;
	
	public ReceiptCopyAction(){
		super(LanguageLoader.getString("Copy"), ImageManager.getImageIconByShortName("copy_edit.gif"));
		putValue(SHORT_DESCRIPTION, LanguageLoader.getString("Copy a receipt."));
		putValue(ACTION_COMMAND_KEY ,LanguageLoader.getString("ReceiptCopyAction"));
		
		this.setEnabled(false);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		//DataSource
		PassbookTable table = (PassbookTable) this.getValue("PassbookTable");
		if(table == null){
			return;
		}
		
		final ReceiptForm dataSource = (ReceiptForm)table.getSelectedObject();
		
		Transferable trans = new Transferable() {
			DataFlavor df = new DataFlavor(ReceiptForm.class,
					"ReceiptForm");

			public DataFlavor[] getTransferDataFlavors() {
				return new DataFlavor[] {df,DataFlavor.stringFlavor};
			}

			public boolean isDataFlavorSupported(DataFlavor flavor) {
				if(flavor.equals(df)){
					return true;
				}
				if(flavor.equals(DataFlavor.stringFlavor)){
					return true;
				}
				return false;
			}

			public Object getTransferData(DataFlavor flavor)
					throws UnsupportedFlavorException, IOException {
				if (flavor.equals(df)) {
					return dataSource;
				}
				if(flavor.equals(DataFlavor.stringFlavor)){
					if(dataSource != null){dataSource.toString();}
					return "";
				}
				throw new UnsupportedFlavorException(flavor);
			}

		};
		Toolkit.getDefaultToolkit().getSystemClipboard()
				.setContents(trans, null);
		
		//Enable ReceiptPasteAction.
		ActionManager.getAction(ReceiptPasteAction.class).setEnabled(
				true);
	}
}
